﻿<?php
/**
* Price Configuration
*/
class Admin_PriceController extends Zend_Controller_Action{
		
	function init(){
            // System_Template::using('blank','Admin');
			System_Template::using('globalconfig','Admin');
			
			// L?y các parameter trong request URL
            $this->Params = $this->getRequest()->getParams();            
            $this->view->maintitle = "Price Configuration";
            $this->model = new Admin_Model_Price();
			$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH.'/log.txt');
			$this->logger = new Zend_Log($writer);
            parent::init();
	}
	
	/**
    * List Price Configuration
    */
	function indexAction(){
		$this->view->data = $this->model->fetchAll();
	}
	
	/**
    * INVISIBLE
	* Delete a Price
    */
	function deleteAction() {
        $db = $this-> model -> getDefaultAdapter();
        $itemID = $this->Params['itemID'];
		
		try{
			$this->model->delete($itemID);
			echo 'successful';
		} catch (Zend_Exception $e){
			echo $e->getMessage();;
		}
        exit;
    }
	
	/**
    * INVISIBLE
	* Redirect to add or edit form
    */
	function inputAction(){
		$id = (isset($this->Params['itemID'])?$this->Params['itemID']:0);
		/* Trường hợp cập nhật */
		if($id>0){
			$data = $this->model->find($id)->current();
			$this->view->data = $data;
		} 
		$this->_helper->layout->disableLayout();
	}
	
	/**
    * INVISIBLE
	* Redirect to add or edit form
    */
	function saveAction(){
		$id = (isset($this->Params['PRICEID'])?$this->Params['PRICEID']: '');
		if($id == ''){
			try{
				$this->Params['PRICEID'] = $this->model->addNewPrice($this->Params);
				$this->view->item = $this->Params;
				$this->view->operation = "add";
			} catch(Zend_Exception $e) {
				echo $e->getMessage();
			}
		} else {
			try{
				$this->model->updatePrice($this->Params);
				$this->view->item = $this->Params;
				$this->view->operation = "edit";
			} catch(Zend_Exception $e) {
				echo $e->getMessage();
			}
			
		}
		$this->_helper->layout->disableLayout();
	}
}